package com.xw.tools.view;

import cn.hutool.json.JSONUtil;
import com.xw.tools.model.TextItemVo;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class JsonFormatWin extends AbstractWin {

    public void start() {
        Stage primaryStage = new Stage();
        primaryStage.setTitle("JsonFormat");
        Scene scene = new Scene(new VBox(), 800, 495);
        initView(scene, primaryStage);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initView(Scene scene, Stage primaryStage) {
        TextArea inputTextArea = new TextArea();
        inputTextArea.setWrapText(true);
        TextArea outputTextArea = new TextArea();
        outputTextArea.setWrapText(true);
        outputTextArea.setFont(Font.font(16));
        Button button = new Button("格式化");
        button.setOnAction((ActionEvent t) -> {
            String inputText = inputTextArea.getText();
            if (inputText == null || inputText.isEmpty()) {
                return;
            }
            // json格式化
            String jsonStr = JSONUtil.formatJsonStr(inputText);

            outputTextArea.setText(jsonStr);
        });

        VBox box = (VBox) scene.getRoot();
        VBox.setVgrow(inputTextArea, Priority.ALWAYS);
        box.getChildren().add(inputTextArea);
        HBox hBox = new HBox();
        box.getChildren().add(hBox);
        box.getChildren().add(button);
        VBox.setVgrow(outputTextArea, Priority.ALWAYS);
        box.getChildren().add(outputTextArea);
    }

    @Override
    public void show() {
        start();
    }
}
